package de.servicezombie.lang;

public class ThreadUtils {

	/**
	 * a null safe pop operation on a threadLocalVariable
	 * @return value of variable or null if non available
	 */
	public static <T> T getAndRemoveThreadLocal(ThreadLocal<T> variable) {
		if(variable == null) {
			return null;
		}
		
		T value = variable.get();
		if(value != null) {
			variable.remove();
		}
		
		return value;
	}

	/**
	 * simply creates a thread and starts it. Used to start async work in a one liner.
	 * @return started thread
	 */
	public static Thread runAsync(Runnable runnable) {
		Thread thread = new Thread(runnable);
		thread.start();
		return thread;
	}
	
}
